import {_decorator, Component, log, Node, quat, Quat} from 'cc';
import {NodeSpace} from "../../../engine_1.2.0/cocos/core/scene-graph/node-enum";

const { ccclass, property } = _decorator;


const quat_x = quat();
Quat.fromEuler(quat_x,2,0,0);

const quat_y = quat();
Quat.fromEuler(quat_y,0,2,0);

const quat_z = quat();
Quat.fromEuler(quat_z,0,0,2);


const quat_t = quat();


@ccclass('TestRotate')
export class TestRotate extends Component {
    /* class member could be defined like this */
    // dummy = '';

    /* use `property` decorator if your want the member to be serializable */
    // @property
    // serializableDummy = 0;

    @property({type:Node})
    nd_ball:Node = null;

    updateFun:Function = null;

    start () {
        // Your initialization goes here.
        log('globalThis:',globalThis);
        globalThis.test_rotate = this;
    }

    ra(code){
        switch (code) {
            case 1:
                // 绕世界坐标系的X轴旋转
                this.updateFun = ()=>{
                    this.nd_ball.rotate(quat_x,Node.NodeSpace.WORLD);
                };
                break;
            case 2:
                // 绕世界坐标系的Y轴旋转
                this.updateFun = ()=>{
                    this.nd_ball.rotate(quat_y,Node.NodeSpace.WORLD);

                    const eulur = this.nd_ball.eulerAngles;
                    Quat.fromEuler(quat_t,eulur.x,eulur.y,eulur.z);
                    log('lrot:',quat_t);
                    debugger;
                };
                break;
            case 3:
                // 绕世界坐标系的X轴旋转
                this.updateFun = ()=>{
                    this.nd_ball.rotate(quat_z,Node.NodeSpace.WORLD);
                };
                break;
            case 4:
                // 绕世界坐标系的X轴旋转
                this.updateFun = ()=>{
                    this.nd_ball.rotate(quat_x,Node.NodeSpace.LOCAL);
                };
                break;
            case 5:
                // 绕世界坐标系的X轴旋转
                this.updateFun = ()=>{
                    this.nd_ball.rotate(quat_y,Node.NodeSpace.LOCAL);
                };
                break;
            case 6:
                // 绕世界坐标系的X轴旋转
                this.updateFun = ()=>{
                    this.nd_ball.rotate(quat_z,Node.NodeSpace.LOCAL);
                };
                break;
            case 7:
                //
                this.updateFun = ()=>{
                    const worldRot = this.nd_ball.worldRotation;
                    Quat.multiply(quat_t,quat_y,worldRot);
                    this.nd_ball.setWorldRotation(quat_t);


                    const eulur = this.nd_ball.eulerAngles;
                    Quat.fromEuler(quat_t,eulur.x,eulur.y,eulur.z);
                    log('lrot:',quat_t);
                    debugger

                    // this.nd_ball.setWorldRotation(quat_t);
                };

                // log(' let worldRot = this.nd_ball.worldRotation;',this.nd_ball.worldRotation);
                // let eulur = this.nd_ball.eulerAngles;
                // let quat_3 = quat();
                // Quat.fromEuler(quat_3,eulur.x,eulur.y,eulur.z);
                // log('lrot:',quat_3);

                break;
            default:
                break;
        }
    }

    update (deltaTime: number) {
        // Your update function goes here.
        if(this.updateFun){
            this.updateFun();
        }
    }
}
